//
//  SLCAActionController.m
//  Objective-C
//
//  Created by 陆承东 on 2018/7/1.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLCAActionController.h"

@interface SLCAActionController ()

@property (nonatomic, weak) UIView *layerView;
@property (nonatomic, weak) CALayer *colorLayer;

@end

@implementation SLCAActionController

- (void)initNavBar {
    self.navTitle = @"CAAction";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)initView {
    UIView *layerView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    layerView.backgroundColor = [UIColor whiteColor];
    self.layerView = layerView;
    [self.view addSubview:layerView];
    
    UIButton *changeButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 160, 100, 30)];
    [changeButton setTitle:@"Chage Color" forState:UIControlStateNormal];
    [changeButton setTitleColor:[UIColor colorWithHexString:@"#66ff66"] forState:UIControlStateNormal];
    changeButton.layer.borderWidth = 1;
    changeButton.layer.borderColor = [UIColor darkGrayColor].CGColor;
    [changeButton addTarget:self action:@selector(changeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [layerView addSubview:changeButton];
    
    // create sublayer
    CALayer *colorLayer = [CALayer layer];
    self.colorLayer = colorLayer;
    self.colorLayer.frame = CGRectMake(50, 50, 100, 100);
    self.colorLayer.backgroundColor = [UIColor blueColor].CGColor;
    // add a custom action
    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    self.colorLayer.actions = @{@"backgroundColor": transition};
    // add it to our view
    [self.layerView.layer addSublayer:self.colorLayer];
}

- (void)changeButtonClick:(UIButton *)button {
    // randomize the layer background color
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    self.colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
}

@end
